/*
 * Copyright (C) 2013 felix
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * DOCUMENTACION: http://documentacion-emple-depart.tk
 */
package adgui.vista;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

/**
 *
 * @author Félix Marín Ramírez | felixmurcia@gmail.com
 * @version 1.0
 */
public final class PanelTabPrincipal extends JFrame {

    private JTabbedPane tabbeds;
    private PanelTabEmple pte;
    private PanelTabDepart ptd;
    private JButton btnSalir;
    private JLabel lbLogoTop;
    private GridBagConstraints gbc;

    /**
     * Constructor de la clase encargado de inicializar todos los elementos del
     * JFrame.
     */
    public PanelTabPrincipal() {
        super("Gestión de Empleados y Departamentos");
        gbc = new GridBagConstraints();
        tabbeds = new JTabbedPane();
        tabbeds.setLayout(new BorderLayout());
        pte = new PanelTabEmple();
        ptd = new PanelTabDepart();
        this.setLayout(new BorderLayout());
        this.crearVentana();
        this.setLabelLogoTop();
        this.addToTabs();
        this.salir();
        this.setVisible(true);
    }

    /**
     * Método encargado de crear la ventana principal.
     */
    private void crearVentana() {
        this.setName("Ventana");        
        this.setMinimumSize(new Dimension(560, 692));
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * Método encargado de insertar el logo de arriba en el JFrame.
     */
    private void setLabelLogoTop() {
        lbLogoTop = new JLabel(new ImageIcon("./img/logoTop.jpg"));
        lbLogoTop.setBorder(new EmptyBorder(0, 0, 5, 0));
        this.getContentPane().add(lbLogoTop, BorderLayout.NORTH);
    }

    /**
     * Método encargado de añadir los paneles tab al panel principal.
     */
    private void addToTabs() {
        tabbeds = new JTabbedPane();
        tabbeds.addTab("Empleados", pte.getJPanel());
        tabbeds.addTab("Departamentos", ptd.getJPanel());
        this.getContentPane().add(tabbeds, BorderLayout.CENTER);
    }

    /**
     * Método encargado de crear la parte inferior de la ventana principal, con
     * el logo de la empresa y el botón de salir.
     */
    private void salir() {
        JPanel panelB = new JPanel();
        GridBagLayout gbl = new GridBagLayout();
        gbl.setConstraints(panelB, gbc);
        panelB.setLayout(gbl);
        //---//
        JPanel panelC = new JPanel();
        panelC.setLayout(new BorderLayout());
        panelC.add(panelB, BorderLayout.EAST);
        //---//        
        this.setRestricciones(0, 0, 1, 20, 10);
        JLabel logo = new JLabel(new ImageIcon("./img/logo.png"));
        logo.setToolTipText("Company Name Inc.");
        panelB.add(logo, gbc);
        //---//
        this.setRestricciones(1, 0, 1, 95, 10);
        panelB.add(new JLabel(), gbc);
        this.setRestricciones(2, 0, 1, 20, 10);
        btnSalir = new JButton("Salir");
        btnSalir.setMnemonic(KeyEvent.VK_S);
        panelB.add(btnSalir, gbc);
        this.getContentPane().add(panelC, BorderLayout.SOUTH);
        //---//        
        btnSalir.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    /**
     * Método encargado de establecer los parámetros de las varables que
     * establecerán las posiciones de los elementos en el JPanel.
     *
     * @param gridx Posicione X con respecto a la esquna superior izquierda del
     * JPanel.
     * @param gridy Posicione Y con respecto a la esquna superior izquierda del
     * JPanel.
     * @param gridwidth Número de columnas que ocupará el elemento dentro del
     * GridBagLayout del JPanel.
     * @param ipady Ancho que tendrá el elemento.
     */
    private void setRestricciones(int gridx, int gridy, int gridwidth, int ipadx, int ipady) {
        gbc.insets = new Insets(3, 3, 3, 8);
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridheight = 1;
        gbc.gridwidth = gridwidth;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.ipadx = ipadx;
        gbc.ipady = ipady;
    }
}
